﻿using Youz.Common.BLL;
using Youz.Common.Entity;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace Youz.CreditRepay.Manage
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            ConnectSignalR.ConnectAsync();
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            string strError = Lang_LanguageBLL.LanguageByManage(LanguageModule.Log_ErrorLog, "GlobalErrorMessage", "系统繁忙，请稍后重试");

            Exception ex = HttpContext.Current.Server.GetLastError();

            if (ex is HttpRequestValidationException)
            {
                strError = Lang_LanguageBLL.LanguageByManage(LanguageModule.Log_ErrorLog, "HttpRequestValidationException", "系統檢測到您提交的數據中含有特殊字符，請修改後再重新提交！");
            }

            SqlException sqlException = null;
            if (ex is SqlException)
            {
                sqlException = ex as SqlException;
            }
            else
            {
                sqlException = ex.InnerException as SqlException;
            }
            if (sqlException != null && sqlException.Number == 229)
            {
                //当前数据库用户为只读权限
                strError = Lang_LanguageBLL.LanguageByManage(LanguageModule.M_Common, "applicationError1", "系統當前正在進行數據結算，目前只能查看數據！");
            }

            try
            {
                Log_ErrorLog log = new Log_ErrorLog();
                log.ErrorLogID = Guid.NewGuid();
                log.Host = HttpContext.Current.Request.Url.Host;
                log.RequestType = HttpContext.Current.Request.RequestType;
                log.LoginType = (int)Utility.CurrentSiteType;
                log.AbsolutePath = HttpContext.Current.Request.Url.AbsolutePath;
                log.PostData = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.Form.ToString()); ;
                log.QueryString = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.QueryString.ToString());
                log.UserHostAddress = Youz.Common.Helper.IPHelp.GetClientIPv4Address();
                log.UserAgent = HttpContext.Current.Request.UserAgent;
                log.Source = ex.Source;                     //导致错误的应用程序或对象的名称
                log.TargetSite = ex.TargetSite.ToString();  //引发当前异常的方法
                log.ErrMess = HttpContext.Current.Server.HtmlEncode(ex.Message);
                log.BaseErrMess = HttpContext.Current.Server.HtmlEncode(ex.GetBaseException().Message);
                log.StackTraceErr = ex.GetBaseException().StackTrace + "\r\n\r\nPostData:\r\n" + HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.Form.ToString());
                log.CreateTime = ConfigParameters.SysTime;
                new Log_ErrorLogBLL().Add(log);
            }
            catch { }

            #if DEBUG

            #else
            //针对Ajax报错的处理；
            if (Youz.Common.Helper.CommonHelper.IsAjaxRequest())
            {
                Response.Write(Utility.AjaxResult(false, strError));
                Response.End();
            }

            HttpException httpe = ex as HttpException;
            if (httpe != null && httpe.WebEventCode == 3004)//针对图片报错
            {
                strError = Lang_LanguageBLL.LanguageByWebSite(LanguageModule.M_Other, "Application_Error2", "添加失败。原因：文件大小不能超过5MB");
                //Response.Write(Utility.AjaxResult(false, strError));
                //Response.End();
            }
            Response.Write(Utility.AjaxResult(false, strError));
            Response.End();
            #endif
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}